
<!DOCTYPE html>
<html>
<head>
<meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport">
<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
<title>demo</title>
<style type="text/css">
.content{
	display: block;
	width:1000px;
	margin: 0 auto;
}
.content>*{
	display: block;
	width:500px;
	margin: 10px auto;
}
.center{
	margin: 10px auto;
	text-align: center;
}
textarea.console{
	display: block;
	height: 500px;
	width:  500px;
	resize:vertical;
/*	none：用户不能操纵机制调节元素的尺寸；
	both：用户可以调节元素的宽度和高度；
	horizontal：用户可以调节元素的宽度；
	vertical：让用户可以调节元素的高度；
	inherit：默认继承。*/
}
</style>
</head>
<body>
<div class="content">
	<!-- 主要知识点-->
	<h1>BOM浏览器对象模型</h1>
	<!-- title以及点击操作区域 -->
	<h1 class="center" onclick="show()" dir="rtl">show</h1>
	<!-- 文本输入操作区域 -->
	<input type="text" id="input" value="" onblur='changeVal()'/>
	<!-- 按钮操作区域 -->
	<input type="button" value="loopA()" onclick="loopAStart()" />
	<!-- 按钮操作区域 -->
	<input type="button" value="getAnswer()" onclick="getAnswer()" />
	<!-- 按钮操作区域 -->
	<input type="button" value="showArr()" onclick="showArr()" />
	<!-- 结果显示区域 -->
	<textarea id="console" class="console" readonly="readonly"></textarea>
</div>
<script type="text/javascript">
var count = 0;
var inputVal = 0;
var ele = document.getElementById('console');	
var inputEle = document.getElementById('input');	
function Log(xxx){
	count++;
	// count = 0;
	//console.log(xxx);
	// 更新数据
	ele.value+=count+":"+xxx+"\n";
	// 始终保持最底部
	ele.scrollTop = ele.scrollHeight;
};

function changeVal(){
	inputVal = parseInt(""+inputEle.value);
}

var resultArr = new Array();  
var maxproduct = 0;

function showArr(){
	for (var i = 0; i < resultArr.length; i++) {
		Log('showArr'+(i+1)+'-----'+resultArr[i]);
	}
}

function loopAStart(){
	resultArr = new Array();
	loopA(inputVal);
}

function loopA(){
	var totalCount = arguments[0];

	var haveCount = 0;

	var lastArr = null;

	if (arguments.length > 1) {
		lastArr = arguments[1];
	}else{
		lastArr = new Array(); 
	}

	for (var i = 0; i < lastArr.length; i++) {
		haveCount=haveCount+lastArr[i];
	}
	if (haveCount>=totalCount) {
		//Log(lastArr);
		resultArr[resultArr.length]=lastArr;
		return false;
	}

	for (var i = 1; i <= totalCount-haveCount; i++) {
		if ( (haveCount + i) > totalCount) {
			continue;
		}
		loopA(totalCount,lastArr.concat(i));
	}
}

function countApple(nums){

	var totalCount=1;
	for(var i=0;i<nums.length;i++){
		totalCount = totalCount*parseInt(""+nums[i]);
	}
	return totalCount;
}

function getAnswer(){
	loopAStart();
	var product = 0;
	for (var i = 0; i < resultArr.length; i++) {
		
		product = countApple(resultArr[i]);

		maxproduct = (maxproduct>=product)?maxproduct:product;

		Log('index--:'+(i+1)+'--arr--:'+resultArr[i]+'--result--:'+product+'maxproduct:-------'+maxproduct);
	}
}


</script>
</body>

</html>